﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Threading;
using System.Threading.Tasks;
using Cef.Utility;
using Cef.Utility.Helper;
using Newtonsoft.Json;

namespace CefSharp.MinimalExample.WinForms
{
    public delegate void ReportDisplayHandler();
    public class BoundObject
    {
        public string MyProperty { get; set; } = "";
        public string PdfUrl { get; set; }
        public ReportDisplayHandler ReportDisplay;
        public string EchoMyProperty()
        {
            return MyProperty;
        }

        public void PrintPDF(IJavascriptCallback javascriptCallback)
        {
            var task=Task.Factory.StartNew(()=>{
                var result=PrinterHelper.DownLoadAndPrint(PdfUrl);
                javascriptCallback.ExecuteAsync(JsonConvert.SerializeObject(result));
            });
            task.Wait();
        }

        public void ReadIdCardInfo(IJavascriptCallback javascriptCallback)
        {
            var task = Task.Factory.StartNew(() =>
            {
                using (javascriptCallback)
                {
                    string response;
                    try
                    {
                        var result = HuaXuReader.ReadId();
                        response = JsonConvert.SerializeObject(result);
                    }
                    catch (Exception e)
                    {
                        response = JsonConvert.SerializeObject(new { e.Message });
                    }
                    javascriptCallback.ExecuteAsync(response);
                }
            });

            task.Wait();
        }

        public void ReadIcCardInfo(IJavascriptCallback javascriptCallback)
        {
            var task = Task.Factory.StartNew(() =>
            {
                using (javascriptCallback)
                {
                    string response;
                    try
                    {
                        var result = HuaXuReader.ReadIc();
                        response = JsonConvert.SerializeObject(result);
                    }
                    catch (Exception e)
                    {
                        response = JsonConvert.SerializeObject(new { e.Message });
                    }
                    javascriptCallback.ExecuteAsync(response);
                }
            });

            task.Wait();
        }
        public void WirteIcCard(IJavascriptCallback javascriptCallback)
        {
            var task = Task.Factory.StartNew(() =>
            {
                using (javascriptCallback)
                {
                    string response;
                    try
                    {
                        if (!string.IsNullOrEmpty(MyProperty))
                        {
                            var result = HuaXuReader.WriteIc(MyProperty);
                            response = JsonConvert.SerializeObject(result);
                        }
                        else
                        {
                            response = JsonConvert.SerializeObject(new{ Message="请输入10个字符以内的文本" });
                        }
                        
                    }
                    catch (Exception e)
                    {
                        response = JsonConvert.SerializeObject(new { e.Message });
                    }
                    javascriptCallback.ExecuteAsync(response);
                }
            });

            task.Wait();
        }
        public void ShowReportForm(IJavascriptCallback javascriptCallback)
        {
            var task = Task.Factory.StartNew(() =>
            {
                using (javascriptCallback)
                {
                    string response;
                    try
                    {
                        if (!string.IsNullOrEmpty(MyProperty))
                        {
                            //Thread newThread = new Thread(new ThreadStart(OpenReportForm));
                            ////ApartmentState Property is obsolete;
                            ////newThread.SetApartmentState(ApartmentState.STA);

                            //// The following line is ignored since
                            //// ApartmentState can only be set once.
                            //newThread.SetApartmentState(ApartmentState.STA);
                            //Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                            //    newThread.ThreadState, newThread.ApartmentState);
                            //newThread.Start();
                            ReportDisplay();
                            response = JsonConvert.SerializeObject(new { Message = "打开报表成功" });
                        }
                        else
                        {
                            response = JsonConvert.SerializeObject(new{ Message="请输入10个字符以内的文本" });
                        }
                        
                    }
                    catch (Exception e)
                    {
                        response = JsonConvert.SerializeObject(new { e.Message });
                    }
                    javascriptCallback.ExecuteAsync(response);
                }
            });

            task.Wait();
        }

        public void FindComputerResourceUsage(IJavascriptCallback javascriptCallback)
        {
            var task = Task.Factory.StartNew(() =>
            {
                using (javascriptCallback)
                {
                    string response = JsonConvert.SerializeObject(SystemInfo.Current);
                    javascriptCallback.ExecuteAsync(response);
                }
            });

            task.Wait();
        }

        public void FindComputerInfo(IJavascriptCallback javascriptCallback)
        {
            var task = Task.Factory.StartNew(() =>
            {
                using (javascriptCallback)
                {
                    Computer computer = new Computer();
                    string response = JsonConvert.SerializeObject(new
                    {
                        cpu_id = computer.CPU_Id,
                        disk_id = computer.Disk_Id,
                        host_name = computer.HostName,
                        networkcard = computer.NetworkCard,
                        serialNumber = computer.SerialNumber_Manufacturer_Product.Item1,
                        manufacturer = computer.SerialNumber_Manufacturer_Product.Item2,
                        product = computer.SerialNumber_Manufacturer_Product.Item3,
                    });
                    javascriptCallback.ExecuteAsync(response);
                }
            });

            task.Wait();
        }

        public void FindUsbDevices(IJavascriptCallback javascriptCallback)
        {
            var task = Task.Factory.StartNew(() =>
            {
                using (javascriptCallback)
                {
                    var devices = new List<UsbDeviceInfo>();

                    ManagementObjectCollection collection;
                    using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%"""))
                        collection = searcher.Get();

                    foreach (var device in collection)
                    {
                        devices.Add(new UsbDeviceInfo(
                            (string)device.GetPropertyValue("DeviceID"),
                            (string)device.GetPropertyValue("PNPDeviceID"),
                            (string)device.GetPropertyValue("Description")
                        ));
                    }

                    collection.Dispose();

                    string response = JsonConvert.SerializeObject(devices);
                    javascriptCallback.ExecuteAsync(response);
                }
            });

            task.Wait();
        }
        
    }
}